package cn.iocoder.yudao.module.handguard.controller.app.applicationdeductrecord;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.handguard.controller.app.applicationdeductrecord.vo.*;
import cn.iocoder.yudao.module.handguard.dal.dataobject.applicationdeductrecord.ApplicationDeductRecordDO;
import cn.iocoder.yudao.module.handguard.convert.applicationdeductrecord.ApplicationDeductRecordConvert;
import cn.iocoder.yudao.module.handguard.service.applicationdeductrecord.ApplicationDeductRecordService;

@Tag(name = "管理后台 - 产品订单-提成明细记录")
@RestController
@RequestMapping("/hutai/application-deduct-record")
@Validated
public class ApplicationDeductRecordController {

    @Resource
    private ApplicationDeductRecordService applicationDeductRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建产品订单-提成明细记录")
    @PreAuthorize("@ss.hasPermission('hutai:application-deduct-record:create')")
    public CommonResult<Long> createApplicationDeductRecord(@Valid @RequestBody ApplicationDeductRecordCreateReqVO createReqVO) {
        return success(applicationDeductRecordService.createApplicationDeductRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新产品订单-提成明细记录")
    @PreAuthorize("@ss.hasPermission('hutai:application-deduct-record:update')")
    public CommonResult<Boolean> updateApplicationDeductRecord(@Valid @RequestBody ApplicationDeductRecordUpdateReqVO updateReqVO) {
        applicationDeductRecordService.updateApplicationDeductRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除产品订单-提成明细记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hutai:application-deduct-record:delete')")
    public CommonResult<Boolean> deleteApplicationDeductRecord(@RequestParam("id") Long id) {
        applicationDeductRecordService.deleteApplicationDeductRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得产品订单-提成明细记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hutai:application-deduct-record:query')")
    public CommonResult<ApplicationDeductRecordRespVO> getApplicationDeductRecord(@RequestParam("id") Long id) {
        ApplicationDeductRecordDO applicationDeductRecord = applicationDeductRecordService.getApplicationDeductRecord(id);
        return success(ApplicationDeductRecordConvert.INSTANCE.convert(applicationDeductRecord));
    }

    @GetMapping("/list")
    @Operation(summary = "获得产品订单-提成明细记录列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('hutai:application-deduct-record:query')")
    public CommonResult<List<ApplicationDeductRecordRespVO>> getApplicationDeductRecordList(@RequestParam("ids") Collection<Long> ids) {
        List<ApplicationDeductRecordDO> list = applicationDeductRecordService.getApplicationDeductRecordList(ids);
        return success(ApplicationDeductRecordConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得产品订单-提成明细记录分页")
    @PreAuthorize("@ss.hasPermission('hutai:application-deduct-record:query')")
    public CommonResult<PageResult<ApplicationDeductRecordRespVO>> getApplicationDeductRecordPage(@Valid ApplicationDeductRecordPageReqVO pageVO) {
        PageResult<ApplicationDeductRecordDO> pageResult = applicationDeductRecordService.getApplicationDeductRecordPage(pageVO);
        return success(ApplicationDeductRecordConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出产品订单-提成明细记录 Excel")
    @PreAuthorize("@ss.hasPermission('hutai:application-deduct-record:export')")
    @OperateLog(type = EXPORT)
    public void exportApplicationDeductRecordExcel(@Valid ApplicationDeductRecordExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ApplicationDeductRecordDO> list = applicationDeductRecordService.getApplicationDeductRecordList(exportReqVO);
        // 导出 Excel
        List<ApplicationDeductRecordExcelVO> datas = ApplicationDeductRecordConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "产品订单-提成明细记录.xls", "数据", ApplicationDeductRecordExcelVO.class, datas);
    }

}
